import { camelCase, lowerFirst } from 'lodash-es'
import {
  changSwaggerData,
  changeProperties,
  flatDefinitions,
  getEntityMethodName,
  getMethodsParams,
  getParamsProperties,
  getSwaggerResponse,
  getSwaggerTree,
} from '@/utils/swaggerUtil/swaggerData'

export function changeTableName(fileLabel) {
  let str = ''
  if (fileLabel?.indexOf('Controller') > -1) {
    str = lowerFirst(fileLabel.replace(' Controller', '').replace(/\s*/g, ''))
  } else {
    str = lowerFirst(fileLabel) || 'default'
  }
  return camelCase(str)
}

//  生成实体与api映射文件
export function mapApiModel(params, tags, definitions, folder = 'external') {
  let treeInfo = getSwaggerTree(params.json)
  tags = tags || []
  definitions = definitions || []
  if (!treeInfo || !treeInfo.data || !tags || !definitions) {
    return []
  }

  for (let i in treeInfo.data) {
    treeInfo['data'][i] = treeInfo['data'][i].map((x) => {
      let paramsList = getParamsProperties(x, definitions)
      let responseList = getSwaggerResponse(x.responses, definitions)

      if (paramsList.length) {
        x.params = paramsList
      }

      let pathList = x.path.split('/')
      // 取倒数第二个
      let structName = pathList.length >= 2 ? pathList[pathList.length - 2] : ''
      if (x.apiName === structName && pathList.length) {
        structName = pathList.length > 3 ? pathList[pathList.length - 3] : pathList[0]
      }

      let methodList = getEntityMethodName(x.method, x.apiName, structName)

      return {
        method: x.method,
        name: methodList,
        path: x.path,
        params: x.params,
        methodName: x.summary,
        response: responseList,
      }
    })
  }

  let modelList = tags.map((x) => {
    let defaultPath = treeInfo['data'][x.name][0]?.url?.split('/')
    if (!x.description && defaultPath.length) {
      x.description = defaultPath.length > 1 ? defaultPath[2] : defaultPath[1]
    }
    let tableName = changeTableName(x.description || 'default')

    return {
      tableCnName: x.name,
      tableName,
      moduleName: x.methodName,
      apiList: treeInfo['data'][x.name] ? treeInfo['data'][x.name] : [],
      path: `/src/api/${folder}/${tableName}/index.js`,
    }
  })
  return modelList
}
//  生成实体与api映射文件
export function handleMapApiModel(params, tags, definitions, folder = 'external') {
  tags = tags || []
  definitions = definitions || []
  let treeInfo = getSwaggerTree(params.json)

  if (!treeInfo || !treeInfo.data) {
    return []
  }
  let modelList = []
  for (let i in treeInfo.data) {
    let curTagInfo = tags?.find(x => `${x.name}` === `${i}`)

    let modelItem = {}
    let tableName = 'default'
    let apiList = getApiList(treeInfo['data'][i], definitions)
    if (!curTagInfo) {
      modelItem = {
        tableCnName: i,
        tableName: changeTableName(i) || 'default',
        moduleName: i,
        apiList,
        path: `/src/api/${folder}/${tableName}/index.js`,
      }
    } else {
      tableName = changeTableName(curTagInfo.description) || 'default'

      modelItem = {
        tableCnName: curTagInfo?.name || i,
        tableName,
        moduleName: i,
        apiList,
        path: `/src/api/${folder}/${tableName}/index.js`,
      }
    }
    modelList.push(modelItem)
  }
  return modelList
}

export function getApiList(list, definitions) {
  return list.map((x) => {
    let paramsList = getParamsProperties(x, definitions)
    let responseList = getSwaggerResponse(x.responses, definitions)

    if (paramsList.length) {
      x.params = paramsList
    }
    // debugger
    let pathList = x.path.split('/')
    // 取倒数第二个
    let structName = pathList.length >= 2 ? pathList[pathList.length - 2] : ''
    if (x.apiName === structName && pathList.length) {
      structName = pathList.length > 3 ? pathList[pathList.length - 3] : pathList[0]
    }
    let methodList = getEntityMethodName(x.method, x.apiName, structName)

    return {
      method: x.method,
      name: methodList,
      path: x.path,
      params: x.params,
      methodName: x.summary,
      response: responseList,
    }
  })
}
